using System;
using RomanJendrusz.Amazon.ETL.Domain;
using RomanJendrusz.Amazon.ETL.Infrastructure.DataBase;
using System.Linq;

namespace RomanJendrusz.Amazon.ETL.Repositories
{
    public class CustomerRepository: Repository<Customer, string>, ICustomerRepository
    {
        public CustomerRepository(ISession session)
            : base(session, s => s.Customers, "CustomerId")
        {
        }

        public Customer AttachIfAllreadyExists(Customer item)
        {
            if(item == null)
            {
                return null;
            }
            var persistedCustomer = session.Customers.SingleOrDefault(c => c.CustomerId == item.CustomerId);
            if (persistedCustomer != null)
            {
                return persistedCustomer;
            }
            return item;
        }

        private void clone(Customer attached, Customer orginal)
        {
            if (string.IsNullOrEmpty(attached.BirthDay) == false)
                orginal.BirthDay = attached.BirthDay;
            if (string.IsNullOrEmpty(attached.City) == false)
                orginal.City = attached.City;
            if (string.IsNullOrEmpty(attached.Country) == false)
                orginal.Country = attached.Country;
            if (string.IsNullOrEmpty(attached.CustomerName) == false)
                orginal.CustomerName = attached.CustomerName;
            if (string.IsNullOrEmpty(attached.Location) == false)
                orginal.Location = attached.Location;
            if (string.IsNullOrEmpty(attached.Nickname) == false)
                orginal.Nickname = attached.Nickname;
            if (string.IsNullOrEmpty(attached.State) == false)
                orginal.State = attached.State;
        }
    }
}